home *** CD-ROM | disk | FTP | other *** search
- /*
- File: usbprint.c
-
- Contains: usb printer class device communication
- (installed in UnitTable)
-
- Version: xxx put version here xxx
-
-
-
- Copyright: 1998 by Apple Computer, Inc., all rights reserved.
-
- */
- #include "PrinterClassDriver.h"
-
- #ifndef __DEVICES__
- #include <devices.h>
- #endif
-
- #ifndef __FILES__
- #include <files.h>
- #endif
-
- #define kMaskLowByte 0x0FF
-
- extern pascal OSErr DRVRDone( OSErr err, DCtlPtr ctl, IOParamPtr pb );
- static void AbortActive( CntrlParam *pb, DCtlPtr clt );
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: GetPrinterStruct
-
- Input Parameters: IOParamPtr i/o parameter block
-
- Output Parameters:
- usbPrinterPBStruct * pointer to the device
-
- Description:
- Given a device control block, map it to a usb device's data structure
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- static struct usbPrinterPBStruct *
- GetPrinterStruct( DCtlPtr ctl )
- {
- return (struct usbPrinterPBStruct *) ctl->dCtlStorage;
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: Read
-
- Input Parameters:
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- static OSErr
- Read (IOParamPtr pb, DCtlPtr ctl)
- {
- struct usbPrinterPBStruct *usbprint = GetPrinterStruct( ctl );
-
- //
- // if we have a unidirectional interface
- // report a read error
- // (only status supported by unidirectional is Centronics compatible)
- //
- pb->ioResult = paramErr; // assume bad
-
- if ( usbprint != nil )
- {
- if ( usbprint->printerProtocol == kUSBPrinterUnidirectionalProtocol )
- pb->ioResult = readErr;
-
- else if (usbprint->terminating)
- pb->ioResult = abortErr;
-
- else if (usbprint->qread)
- (*usbprint->qread)( pb, ctl, usbprint ); // map the read param block into the usb param block
- }
- return pb->ioResult;
-
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: Write
-
- Input Parameters:
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- static OSErr
- Write (IOParamPtr pb, DCtlPtr ctl)
- {
- struct usbPrinterPBStruct *usbprint = GetPrinterStruct( ctl );
-
- pb->ioResult = paramErr; // assume bad
-
- if ( usbprint != nil )
- {
- if (usbprint->terminating)
- pb->ioResult = abortErr;
-
- else if (usbprint->qwrite)
- (*usbprint->qwrite)( pb, ctl, usbprint ); // map the write param block into the usb param block
- }
- return pb->ioResult;
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: AbortActive
-
- Input Parameters:
- pb
- ctl
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- static void
- AbortActive( CntrlParam *pb, DCtlPtr ctl )
- {
- // we need to wait here until the transaction is complete
- struct usbPrinterPBStruct *usbprint = GetPrinterStruct( ctl );
- struct USBPB *pActiveUSBIO;
- IOParamPtr pActiveIO;
-
- if ( usbprint != nil )
- {
- if ( pb->ioCRefNum == usbprint->outRefNum )
- {
- pActiveUSBIO = &usbprint->out;
- pActiveIO = usbprint->writeDrvr.pb;
- }
- else
- {
- pActiveUSBIO = &usbprint->in;
- pActiveIO = usbprint->readDrvr.pb;
- }
-
- if ( pActiveUSBIO->usbCompletion != nil )
- {
- if (usbprint->qabort != nil )
- (*usbprint->qabort)( pb->ioCRefNum, usbprint ); // cancel outstanding transactions
- //
- // synchronize data toggle
- // USB addendum, the endpoints of the pipe may be out of sync
- // a soft reset in the printer class should restore the data toggle
- //
- if (!(usbprint->terminating) && (usbprint->qstatus != nil))
- {
- pb->csCode = kDrvrSoftReset;
- (*usbprint->qstatus)( pb, ctl, usbprint );
- }
- }
- }
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: DRVROpen
-
- Input Parameters:
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-
- pascal OSErr
- DRVROpen(CntrlParam *pb, DCtlPtr dce)
- {
- return noErr;
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: DRVRClose
-
- Input Parameters:
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-
- pascal OSErr
- DRVRClose(CntrlParam *pb, DCtlPtr ctl)
- {
- return noErr;
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: DRVRStatus
-
- Input Parameters:
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-
- pascal OSErr
- DRVRStatus(CntrlParam *pb, DCtlPtr ctl)
- {
- OSErr err;
- struct usbPrinterPBStruct *usbprint = GetPrinterStruct( ctl );
-
- pb->ioResult = paramErr;
- if ( usbprint != nil )
- {
- if (usbprint->terminating)
- pb->ioResult = abortErr;
- else
- {
- switch ( pb->csCode )
- {
- case kDrvrCentronicsStatus: // USB device: centronics status
- case kDrvr1284IdString: // USB device: 1284 capability string
- case kDrvrSoftReset: // USB device: soft reset
- if ( usbprint->qstatus )
- (*usbprint->qstatus)( pb, ctl, usbprint ); // map the status param block into the usb param block
- break;
- case kDrvrNumDevices:
- pb->ioResult = noErr;
- break;
- case 1:
- case 2: // deprecated
- default:
- pb->ioResult = paramErr;
- break;
- }
- }
- }
- return pb->ioResult;
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: DRVRControl
-
- Input Parameters:
- csCode csParam
- ------ -------
- kDrvrPrivateSetStorage pointer to the USB device class storage
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
-
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-
- pascal OSErr
- DRVRControl(CntrlParam *pb, DCtlPtr ctl)
- {
- OSErr err = noErr;
- struct usbPrinterPBStruct *usbprint = GetPrinterStruct( ctl );
-
- switch ( pb->csCode )
- {
- case killCode:
- //
- // killIO is always handled as an immediate mode transaction
- //
- AbortActive( pb, ctl );
- break;
- case kDrvrPrivateSetStorage:
- //
- // reference the class driver's private storage
- // it's not a handle, but devices.h thinks it should be
- //
- ctl->dCtlStorage = (Handle) *((Ptr *) &pb->csParam[0] );
- break;
- default:
- err = paramErr;
- break;
- }
- return err;
- }
-
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Name: DRVRPrime
-
- Input Parameters:
-
- Output Parameters:
- <none>
-
- Description:
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-
- pascal OSErr
- DRVRPrime(CntrlParam *pb, DCtlPtr ctl)
- {
- OSErr err = paramErr;
- //
- // switch on the low order byte to dispatch reads and writes
- //
- if ( (pb->ioTrap & kMaskLowByte) == aRdCmd )
- err = Read( (IOParamPtr) pb, ctl );
-
- else if ( (pb->ioTrap & kMaskLowByte) == aWrCmd )
- err = Write( (IOParamPtr) pb, ctl );
-
- //
- // get the ioResult in case the completion routine has already executed
- //
- if ( err == noErr )
- err = pb->ioResult;
-
- return err;
- }
-
- // eof
-